package cn.com.headfree.workflow.workflowapprove.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;

import cn.com.headfree.workflow.dto.ApproveWorkFlowDto;
import cn.com.headfree.workflow.workflowapprove.dao.IWorkFlowApproveDao;
import cn.com.headfree.workflow.workflowapprove.model.TWorkFlowApprove;
import cn.com.headfree.workflow.workflowapprove.model.TWorkFlowNodeApprove;
/**
 * @description 工作流审批数据访问实现
 * @author fengya
 * @date 2015-8-6 上午09:16:34
 */
@Repository("workFlowApproveDao")
public class WorkFlowApproveDaoImpl extends BaseDaoImpl<TWorkFlowApprove> implements IWorkFlowApproveDao {

	public List<TWorkFlowApprove> getApproveFlow(String workFlowId, String businessId) {
		return this.findAllBySQL(TWorkFlowApprove.class, "select * from t_work_flow_approve a where a.work_flow_id=? and a.business_id=?", null, false, workFlowId,businessId);
	}

	@Override
	public List<TWorkFlowNodeApprove> getWorkFlowNodeApproveByBusinessId(String businessId) {
		String sql = "select * from t_work_flow_node_approve wfna where exists(select 1 from t_work_flow_approve wfa where wfa.business_id=? and wfa.id=wfna.approve_id)";
		return this.findAllBySQL(TWorkFlowNodeApprove.class, sql, null, false, businessId);
	}
	
	@Override
	public List<TWorkFlowNodeApprove> getWorkFlowNodeApproveByApproveId(String approveId) {
		return this.findAllBySQL(TWorkFlowNodeApprove.class, "select * from t_work_flow_node_approve wfna where wfna.APPROVE_ID=?", null, false, approveId);
	}
	
	public TWorkFlowNodeApprove getWorkFlowNodeApprove(String approveId,String userId){
		return this.findUnique(TWorkFlowNodeApprove.class, "select * from t_work_flow_node_approve wfna where wfna.APPROVE_ID=? and wfna.APPROVER=?", approveId,userId);
	}

	@Override
	public void findWaitApproveWorkFlow(Page<ApproveWorkFlowDto> page, String userId, String sysCode) {
		String sql = "select wfa.work_flow_id,wfa.business_id,wfa.id as approve_id from t_work_flow_info info join t_work_flow_approve wfa on info.sys_code=? and info.id=wfa.work_flow_id join t_work_flow_node_approve wfna on wfna.APPROVE_ID=wfa.id and wfna.APPROVE_STATUS=0 and wfna.APPROVER=?";
		this.findAsPageBySql(ApproveWorkFlowDto.class, sql, page, null, true, sysCode,userId);
	}

	@Override
	public void findApprovedWorkFlow(Page<ApproveWorkFlowDto> page, String userId, String sysCode) {
		String sql = "select wfa.work_flow_id,wfa.business_id,wfa.id as approve_id from t_work_flow_info info join t_work_flow_approve wfa on info.sys_code=? and info.id=wfa.work_flow_id join t_work_flow_node_approve wfna on wfna.APPROVE_ID=wfa.id and wfna.APPROVE_STATUS!=0 and wfna.APPROVER=?";
		this.findAsPageBySql(ApproveWorkFlowDto.class, sql, page, null, true, sysCode,userId);		
	}

}
